package cn.virens.web.controller.manage.platform;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.virens.common.RequestUtil;
import cn.virens.web.common.BaseUserController;
import cn.virens.web.components.beetl.format.ImageFormat;
import cn.virens.web.components.websocket.WebSocketPool;

@Controller
@RequestMapping("/manage/platform/socket")
public class SocketTestContorller extends BaseUserController {
	private @Autowired WebSocketPool mWebSocketPool;

	public SocketTestContorller() {
		super("/manage/platform/socket");
	}

	@RequestMapping("index.jspx")
	public String index(HttpServletRequest request, HttpServletResponse response, Model model) {
		model.addAttribute("sessionid", request.getRequestedSessionId());

		return returnUrl("index");
	}

	@ResponseBody
	@RequestMapping("test.json")
	public Model test(HttpServletRequest request, HttpServletResponse response, Model model) {
		String content = RequestUtil.getValueStr(request, "content");
		String uuid = RequestUtil.getValueStr(request, "uuid");

		JSONObject payload = new JSONObject();
		payload.put("self", false);
		payload.put("name", "测试用户");
		payload.put("content", content);
		payload.put("createTime", DateUtil.now());
		payload.put("uuid", IdUtil.fastSimpleUUID());
		payload.put("avatar", ImageFormat.DEF_AVATAR);

		if (mWebSocketPool.sendUser(uuid, payload) > 0) {
			return renderSuccess(model, "发送成功");
		} else {
			return renderFail(model, "发送失败");
		}
	}

}
